/*
 * Created by JFormDesigner on Sat Jun 17 15:03:01 CST 2023
 */

package fm.customer;

import java.awt.event.*;
import dao.Dao_customer;
import model.Customer;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class UpCustomer extends JInternalFrame {

    String [] columns = {"客户编号", "客户姓名", "联系电话", "地址"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Customer customer = (Customer) list.get(i);

            results[i][0] = customer.getCusID();
            results[i][1] = customer.getCusName();
            results[i][2] = customer.getCusPhone();
            results[i][3] = customer.getCusAddress();
        }

        return results;
    }
    public UpCustomer() {
        initComponents();
        iniDate();
    }

    public void iniDate()
    {
        Object [][] results = getFS(Dao_customer.selectCus());
        DefaultTableModel model = new DefaultTableModel(results, columns);
        table1.setModel(model);
    }

    private void table1MouseClicked(MouseEvent e) {
        int setRow =table1.getSelectedRow();
        if(setRow == -1) return;

        String cl_no = table1.getValueAt(setRow,0).toString().trim();
        String cl_name = table1.getValueAt(setRow,1).toString().trim();
        String cl_phone = table1.getValueAt(setRow,2).toString().trim();
        String cl_address = table1.getValueAt(setRow,3).toString().trim();

        IDTextField.setText(cl_no);
        nameTextField.setText(cl_name);
        phoneTextField.setText(cl_phone);
        addressTextField.setText(cl_address);
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void delete(ActionEvent e) {
        String cusID = IDTextField.getText().trim();
        int i = Dao_customer.deleteCus(cusID);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"删除成功！");

            Object [][] results = getFS(Dao_customer.selectCus());
            DefaultTableModel tableModel = new DefaultTableModel(results,columns);
            table1.setModel(tableModel);
        }
    }

    private void up(ActionEvent e) {
        if(nameTextField.getText().trim().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入姓名!");
            return;
        }

        if(phoneTextField.getText().trim().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入联系电话！");
            return;
        }

        if(addressTextField.getText().trim().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入地址！");
            return;
        }

        String cusID = IDTextField.getText().trim();
        String cusName = nameTextField.getText().trim();
        String cusPhone = phoneTextField.getText().trim();
        String cusAddress = addressTextField.getText().trim();

        int i = Dao_customer.updateCus(cusID, cusName, cusPhone, cusAddress);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"修改成功！");

            Object [][] results = getFS(Dao_customer.selectCus());
            DefaultTableModel tableModel = new DefaultTableModel(results,columns);
            table1.setModel(tableModel);
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        upButton = new JButton();
        deleteButton = new JButton();
        cancelButton = new JButton();
        panel2 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel3 = new JPanel();
        label1 = new JLabel();
        IDTextField = new JTextField();
        label2 = new JLabel();
        nameTextField = new JTextField();
        label3 = new JLabel();
        phoneTextField = new JTextField();
        label4 = new JLabel();
        addressTextField = new JTextField();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(407, 40));
            panel1.setLayout(null);

            //---- upButton ----
            upButton.setText("\u786e\u5b9a");
            upButton.addActionListener(e -> up(e));
            panel1.add(upButton);
            upButton.setBounds(new Rectangle(new Point(154, 6), upButton.getPreferredSize()));

            //---- deleteButton ----
            deleteButton.setText("\u5220\u9664");
            deleteButton.addActionListener(e -> delete(e));
            panel1.add(deleteButton);
            deleteButton.setBounds(new Rectangle(new Point(258, 6), deleteButton.getPreferredSize()));

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(356, 6), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 324, 573, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 180));
            panel2.setBorder(new TitledBorder("\u5ba2\u6237\u4fe1\u606f"));
            panel2.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table1 ----
                table1.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        table1MouseClicked(e);
                    }
                });
                scrollPane1.setViewportView(table1);
            }
            panel2.add(scrollPane1);
            scrollPane1.setBounds(6, 20, 559, 150);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 0, 573, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setBorder(new TitledBorder("\u4fee\u6539\u4fe1\u606f"));
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u5ba2\u6237\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(55, 40, label1.getPreferredSize().width, 18);

            //---- IDTextField ----
            IDTextField.setEditable(false);
            panel3.add(IDTextField);
            IDTextField.setBounds(115, 35, 120, IDTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u59d3\u540d\uff1a");
            panel3.add(label2);
            label2.setBounds(304, 40, label2.getPreferredSize().width, 19);
            panel3.add(nameTextField);
            nameTextField.setBounds(339, 35, 125, nameTextField.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            panel3.add(label3);
            label3.setBounds(54, 95, label3.getPreferredSize().width, 21);
            panel3.add(phoneTextField);
            phoneTextField.setBounds(116, 91, 119, phoneTextField.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u5730\u5740\uff1a");
            panel3.add(label4);
            label4.setBounds(305, 95, 37, 20);
            panel3.add(addressTextField);
            addressTextField.setBounds(340, 91, 124, addressTextField.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 180, 573, 144);

        contentPane.setPreferredSize(new Dimension(585, 395));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton upButton;
    private JButton deleteButton;
    private JButton cancelButton;
    private JPanel panel2;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel3;
    private JLabel label1;
    private JTextField IDTextField;
    private JLabel label2;
    private JTextField nameTextField;
    private JLabel label3;
    private JTextField phoneTextField;
    private JLabel label4;
    private JTextField addressTextField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
